package com.tsing.bms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.bms.entity.Notice;
import com.tsing.bms.mapper.NoticeMapper;
import com.tsing.bms.service.INoticeService;
import com.tsing.bms.vo.req.IdReqVo;
import com.tsing.bms.vo.req.IdsReqVo;
import com.tsing.bms.vo.req.NoticeAddReqVo;
import com.tsing.bms.vo.req.NoticeUpdateReqVo;
import com.tsing.bms.vo.resp.NoticeRespVo;
import com.tsing.common.exceptions.BusinessException;
import com.tsing.common.exceptions.ServerException;
import com.tsing.common.utils.BeanCoperUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Override
    public void addNotice(NoticeAddReqVo reqVo) {
        if (0 != baseMapper.selectCount(new QueryWrapper<Notice>().eq("name", reqVo.getName()))) {
            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
        }
        Notice noTice = new Notice();
        BeanUtils.copyProperties(reqVo, noTice);
        baseMapper.insert(noTice);
    }

    @Override
    public void deleteNotice(IdsReqVo reqVo) {
//        if (!StringUtils.isBlank(baseMapper.haveRelevance(reqVo.getIds()))) {
//            throw new BusinessException(ServerException.Proxy.MAINTAIN_HAVE_RELEVANCE);
//        }
        baseMapper.deleteBatchIds(reqVo.getIds());
    }

    @Override
    public NoticeRespVo getNotice(IdReqVo reqVo) {
        NoticeRespVo noTiceRespVo = new NoticeRespVo();
        BeanUtils.copyProperties(baseMapper.selectById(reqVo.getId()), noTiceRespVo);
        return noTiceRespVo;
    }

    @Override
    public void updateNotice(NoticeUpdateReqVo reqVo) {
        if (0 != baseMapper.selectCount(new QueryWrapper<Notice>().eq("name", reqVo.getName())
                .ne("id", reqVo.getId()))) {
            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
        }
        Notice noTice = new Notice();
        BeanUtils.copyProperties(reqVo, noTice);
        baseMapper.updateById(noTice);
    }

    @Override
    public List<NoticeRespVo> noTiceList() {
        try {
            return BeanCoperUtil.copyObjects(baseMapper.selectList(null), NoticeRespVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
